package com.siwei.mes.job;

import cn.hutool.json.JSONUtil;
import com.siwei.mes.common.base.BaseParams;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.experiment.ExperimentDetail;
import com.siwei.mes.entity.experiment.concrete.batch.ConcreteKsdj;
import com.siwei.mes.enums.ExperimentStatusEnum;
import com.siwei.mes.mapper.experiment.ExperimentDetailMapper;
import com.siwei.mes.mapper.experiment.ExperimentMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: mes-platform
 * @description:
 * @author: yangke
 * @create: 2024-03-17 11:45
 **/
@Component
public class ExperimentStatusJob {
    private static final Logger log = LoggerFactory.getLogger("job");
    @Value("${spring.profiles.active}")
    private String active;
    @Resource
    private ExperimentMapper experimentMapper;
    @Resource
    private ExperimentDetailMapper experimentDetailMapper;

    /**
     * @Scheduled(fixedRate = 3600000)
     */
    //@Scheduled(cron = "0 0/1 * * * ?")
    public void init() {
        /**加在这里里面的配置是不需要执行定时任务的*/
        if (StringUtils.isBlank(active) || BaseParams.LOCAL_LIST.contains(active)) {
            log.info("这里是本地，不会执行定时任务");
            return;
        }
        /**查询试验台账试验中的数据 */
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        log.info("校验试验委托状态值开始");
        List<Experiment> experimentList = experimentMapper.getList(new Experiment().setExperimentStatus(ExperimentStatusEnum.IN_TEST.getCode()));
        for (Experiment experiment : experimentList) {
            if (!ObjectUtils.isEmpty(experiment.getMoldingTime())) {
                /**计算两个日期之间相隔的天数*/
                Long starTime = experiment.getMoldingTime().getTime();
                Long endTime = System.currentTimeMillis();
                Long num = endTime - starTime;
                long day = num / 24 / 60 / 60 / 1000;
                if (day > 7) {
                    experiment.setEntrustState("0");
                    experimentMapper.updateByPrimaryKey(experiment);
                } else {
                    List<ExperimentDetail> experimentDetailList = experimentDetailMapper.selectByExperimentId(experiment.getId(), "CONCRETE_PARAM_KSDJ");
                    if (CollectionUtils.isNotEmpty(experimentDetailList)) {
                        experimentDetailList.stream().sorted(Comparator.comparing(ExperimentDetail::getCreateTime)).collect(Collectors.toList());
                        ExperimentDetail experimentDetail = experimentDetailList.get(0);
                        // 将 JSON 字符串 转成 Java 对象
                        ConcreteKsdj concreteKsdj = JSONUtil.toBean(experimentDetail.getObjJson().toString(), ConcreteKsdj.class);
                        List<ConcreteKsdj.KsInfo> ksInfo = concreteKsdj.getKsInfo();
                        ksInfo.stream().sorted(Comparator.comparing(ConcreteKsdj.KsInfo::getJysj)).collect(Collectors.toList());
                        if (isCheck(ksInfo.get(0))) {
                            experiment.setEntrustState("0");
                            experimentMapper.updateByPrimaryKey(experiment);
                        }
                    }
                }
            }
        }
        log.info("校验试验委托状态值完成", stopWatch.getTotalTimeMillis() / 1000);
    }

    private boolean isCheck(ConcreteKsdj.KsInfo ksInfo) {
        int count = 0;
        String sfssStr = ksInfo.getSfss1() + ksInfo.getSfss2() + ksInfo.getSfss3() + ksInfo.getSfss4() + ksInfo.getSfss5() + ksInfo.getSfss6();
        while (sfssStr.indexOf("是") != -1) {
            sfssStr = sfssStr.substring(sfssStr.indexOf("是") + 1, sfssStr.length());
            count++;
        }
        if (count <= 3) {
            return true;
        }
        return false;
    }
}
